<?php
class Admin_Permission_Resource
{
    public function getOptionArray()
    {
        $config = Bootstrap::getConfig()->toArray();
        $resource = $config['admin']['acl'];
        $result = array(
            'value'=>'all',
            'label'=>'All',
        );

        foreach ($resource as $_item) {
            $children = null;
            if(isset($_item['children'])) {
                $children = $this->_getItem($_item['children']);
            }

            $data = array(
                'value'=>$_item['key'],
                'label'=>$_item['title'],
            );
            if(isset($children)) {
                $data['chields'] = $children;
            }

            $result['chields'][] = $data;
        }

        return array($result);
    }

    protected function _getItem($item)
    {
        $result = array();

        foreach ($item as $_item) {
            if(isset($_item['children'])) {
                $children = $this->_getItem($_item['children']);
            }
            $data = array(
                'value'=>$_item['key'],
                'label'=>$_item['title'],
            );
            if(isset($children)) {
                $data['chields'] = $children;
            }
            $result[] = $data;
        }

        return $result;
    }

    public function checkPermision($action)
    {
        $session = new Model_Admin_Session();
        $permision = $session->getUser()->getPermision();
        $permision = explode(',', $permision);
        $permision = array_fill_keys($permision, true);

        $acl = $this->_getAclItems();

        if(!isset($acl[$action])) {
            return false;
        }

        if(array_key_exists($action, $permision)) {
            return true;
        } else {
            $tempAction = $action;
            while(true) {
                if(array_key_exists($tempAction, $permision)) {
                    return true;
                } else {
                    if(isset($acl[$tempAction]['parent'])) {
                        $tempAction = $acl[$tempAction]['parent'];
                    } else {
                        return false;
                    }
                }
            }
        }
    }

    protected function _getAclItems()
    {
        $config = Bootstrap::getConfig()->toArray();
        $resource = $config['admin']['acl'];

        $resultResource = array();
        $resultResource['all'] = array(
            'label'=>'All',
            'parent'=>null,
        );

        foreach ($resource as $key=>$_item) {
            $resultResource[$_item['key']] = array(
                'label'=>$_item['title'],
                'parent'=>'all',
            );
            if(isset($_item['children'])) {
                $this->_setItemPermision($_item['children'], $_item['key'],$resultResource);
            }
        }

        return $resultResource;
    }

    protected function _setItemPermision($item, $parent, &$resultResource)
    {
        foreach ($item as $_item) {
            $resultResource[$_item['key']] = array(
                'label'=>$_item['title'],
                'parent'=>$parent,
            );
            if(isset($_item['children'])) {
                $this->_setItemPermision($_item, $_item['key'], $resultResource);
            }
        }
    }
}